const { regClass } = Laya;
import { StaticData } from "../../Data/StaticData";
import HttpAPI from "../../HttpManager/HttpAPI";
import HttpManager from "../../HttpManager/HttpManager";
import { ViewRecordBase } from "./ViewRecord.generated";

@regClass()
export class ViewRecord extends ViewRecordBase {
    private currentPage: number = 1;
    private totalPage: number = 0;
    private arrRecordList: Array<any> = [];
    onAwake(): void {
        this.init();
        this.getRecordInfo(this.currentPage);

    }
    init() {
        this.Image.visible = false;
        this.createDataList();

    }
    private getRecordInfo(num: number) {
        HttpManager.post(HttpAPI.POST_USER_RECORDSDETAIL, {
            pageNo: num, pageSize: 8
        }, null).then(this.onGetRecordList.bind(this)).catch(this.onGetRecordList.bind(this));
        this.ChangePage.onChangePage(this.totalPage, this.currentPage);
    }
    private onGetRecordList(data: any) {
        this.arrRecordList = [];
        if (data && data.success) {
            const result = data.result.records;
            this.totalPage = data.result.pages;
            this.currentPage = data.result.current;
            for (let item of result) {
                this.arrRecordList.push({ RecordName: item.name, Time: item.createTime, RecordBg: item.status });
            }
            this.ChangePage.onChangePage(this.totalPage, this.currentPage);
        }
        if (this.arrRecordList.length == 0) {
            this.RecordList.visible = false;
            this.NoneState.visible = true;
        }
        else {
            this.NoneState.visible = false;
            this.RecordList.visible = true;
            this.updataRecordList();
        }
        this.addEvent();
    }
    updataRecordList() {
        this.RecordList.array = this.arrRecordList;
        this.RecordList.repeatY = this.arrRecordList.length / 2;
        if (this.arrRecordList.length < 2) {
            this.RecordList.repeatX = this.arrRecordList.length;
        }
        else {
            this.RecordList.repeatX = 2;
        }
        this.RecordList.refresh();
    }
    createDataList() {
        this.RecordList.renderHandler = Laya.Handler.create(this, this.onRenderRecordList, undefined, false);
        this.RecordList.array = this.arrRecordList;
    }
    onRenderRecordList(item: Laya.Box, index: number) {
        if (index > this.RecordList.length || index < 0) return;
        let recordname = item.getChildByName("RecordName") as Laya.Label;
        let time = item.getChildByName("Time") as Laya.Label;
        let recordbg = item.getChildByName("RecordBg") as Laya.Image;
        const data = this.RecordList.array[index];
        recordname.text = data.RecordName;
        time.text = data.Time;
        if (data.RecordBg == 0) {
            recordbg.skin = "atlas/UI/Shop/box_dhjl_b@1x.png"
        }
        else {
            recordbg.skin = "atlas/UI/Shop/box_dhjl_a@1x.png"
        }
    }

    onRChangePage() {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        if (this.currentPage >= this.totalPage) { return }
        this.currentPage++;
        this.getRecordInfo(this.currentPage);
    }
    onLChangePage() {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        if (this.currentPage <= 1) { return }
        this.currentPage--;
        this.getRecordInfo(this.currentPage);
    }
    onClickChangePage(num: number, event: Laya.Event) {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        let obj = this.ChangePage.getChildByName("PageBox").getChildAt(num).getChildByName("BG").getChildByName("Label") as Laya.Label;
        this.currentPage = parseInt(obj.text);
        this.getRecordInfo(this.currentPage);
    }
    addEvent() {
        this.removeEvent();
        this.BtnClose.on(Laya.Event.CLICK, this, this.onBtnClose)
        this.ChangePage.getChildByName("LPageBG").on(Laya.Event.CLICK, this, this.onLChangePage)
        this.ChangePage.getChildByName("RPageBG").on(Laya.Event.CLICK, this, this.onRChangePage)
        this.QR.on(Laya.Event.CLICK, this, this.onClickQR)
        this.Image.on(Laya.Event.CLICK, this, this.onClickQRC)
        const pageBox = this.ChangePage.getChildByName("PageBox");
        for (let i = 0; i < pageBox.numChildren; i++) {
            const child = pageBox.getChildAt(i);
            child.on(Laya.Event.CLICK, this, this.onClickChangePage, [i]);
        }
    }
    removeEvent() {
        this.BtnClose.off(Laya.Event.CLICK, this, this.onBtnClose)
        this.ChangePage.getChildByName("LPageBG").off(Laya.Event.CLICK, this, this.onLChangePage)
        this.ChangePage.getChildByName("RPageBG").off(Laya.Event.CLICK, this, this.onRChangePage)
        this.QR.off(Laya.Event.CLICK, this, this.onClickQR)
        this.Image.off(Laya.Event.CLICK, this, this.onClickQRC)
        const pageBox = this.ChangePage.getChildByName("PageBox");
        for (let i = 0; i < pageBox.numChildren; i++) {
            const child = pageBox.getChildAt(i);
            child.on(Laya.Event.CLICK, this, this.onClickChangePage, [i]);
        }
    }
    onBtnClose() {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        this.destroy();
    }
    onClickQR() {
        HttpManager.get(HttpAPI.GET_USER_SHOWWRITEOFFCODE, undefined, undefined).then(this.showQR.bind(this)).catch(this.showQR.bind(this))
    }
    showQR(data: any) {
        if (data && data.success) {
            const result = data.result;
            this.QRC.skin=StaticData.checkImageBase64(result.writeOffCode);
        }
        this.Image.visible = true;
    }
    onClickQRC() {
        this.Image.visible = false;
    }
    
}